import 'package:badges/badges.dart' as badges;
import 'package:family_expense/features/system_setting/widgets/about_view.dart';
import 'package:family_expense/features/system_setting/widgets/general_setting_view.dart';
import 'package:family_expense/features/system_setting/widgets/theme_setting_view.dart';
import 'package:flutter/material.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class SystemSettingHome extends StatelessWidget {
  const SystemSettingHome(this.checkUpdateResult, {super.key});

  final CheckAppUpdateResult? checkUpdateResult;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('系统设置')),
      body: _buildBody(context),
    );
  }

  /// Builds main body.
  Widget _buildBody(BuildContext context) {
    return ListView(
      children: [
        ListTile(
          title: const Text('通用'),
          trailing: const ListTileNavigateNextIcon(),
          onTap: () =>
              NavigatorHelper.push(context, const GeneralSettingView()),
        ),
        ListTile(
          title: const Text('主题'),
          trailing: const ListTileNavigateNextIcon(),
          onTap: () => NavigatorHelper.push(context, const ThemeSettingView()),
        ),
        ListTile(
          title: Row(
            children: [
              const Text('关于'),
              const SizedBox(width: 10.0),
              badges.Badge(
                showBadge: checkUpdateResult?.hasUpdate == true,
                badgeAnimation: const badges.BadgeAnimation.scale(),
              ),
            ],
          ),
          trailing: const ListTileNavigateNextIcon(),
          onTap: () => NavigatorHelper.push(context, const AboutView()),
        ),
        const Divider(),
        ListTile(
          title: const Text(
            '退出登录',
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.red),
          ),
          onTap: () => showConfirmDialog(
            context,
            '确认退出当前账号吗？',
            onConfirm: () {
              // Logs out from remote server.
              // Even token is stateless this action clears active token from server.
              // Active token can prevent any token compromise instantly. This is for strict security concern.
              AuthService().logout();
            },
          ),
        ),
      ],
    );
  }
}
